<?php

class SettingController extends Controller {

    public function actionCheckVersion() {
        if (!isset($_GET["version"]) || !isset($_GET["platform"])) {
            JSONHelper::echoJSON(false, JSONHelper::ERROR_ILLEGAL_REQUEST);
        }

        $pf = trim($_GET["platform"]);
        if ($pf == "iphone") {
            $pf = 0;
        } else if ($pf == "android") {
            $pf = 1;
        } else {
            JSONHelper::echoJSON(false, JSONHelper::ERROR_ILLEGAL_REQUEST);
        }

        $nowVersion = trim($_GET["version"]);

        $versionInfo = B2bVersion::model()->findByAttributes(array("platform" => $pf), array("order" => "id desc"));

        $lastestVersion = explode(".", $versionInfo->version);
        
        $nowVersion = explode(".", $nowVersion);
        if(count($lastestVersion)!=3||count($nowVersion)!=3)
        {
            JSONHelper::echoJSON(false, JSONHelper::ERROR_ILLEGAL_REQUEST);
        }
        $upflag = 0;
        foreach($lastestVersion as $key=>$it){
            if($it > $nowVersion[$key]){
                $upflag = 1;
            }
        }
        
        if (!$upflag) {
            $data["isLatest"] = 1;
            $data["latestVersion"] = "";
            $data["appUrl"] = "";
        } else {
            $data["isLatest"] = 0;
            $data["latestVersion"] = $versionInfo->version;
            $data["appUrl"] = $versionInfo->appurl;
        }

        JSONHelper::echoJSON(true, $data);
    }

    public function actionCheckShop() {
        $data["url"] = "http://shop.pocdoc.cn";
        JSONHelper::echoJSON(true, $data);
    }
    
    /**
     * 意见反馈
     */
    public function actionFeedback()
    {
        if(!isset($_POST["content"])){
            JSONHelper::echoJSON(FALSE,  JSONHelper::ERROR_ILLEGAL_REQUEST);
        }
        $fb = new B2bFeedback();
        $fb->telephone = isset($_POST["telephone"])?$_POST["telephone"]:"";
        $fb->content = $_POST["content"];
        $fb->email=isset($_POST["email"])?$_POST["email"]:"";
        $fb->create_at=new CDbExpression("NOW()");
        $fb->save();
        
        $sms = new SmsService();
        $sms->send("18701375406", "反馈:".str_replace(" ", "-", $fb->content).",from:".$fb->telephone);
        JSONHelper::echoJSON(true);
    }


}
